(function () {

	var $modal = $('#pop-modal');
	//提交表格
	$('#submitrv').on('click',function () {

		var sMachName = $('input[name="shebeimc"]').val(); //品牌名字
		var sPinpai = $('select[name="pinpai"]').val(); //品牌
		var sCheXing = $('select[name="chexing"]').val(); //车型
		var dShangPai = $('input[name="shangpaisj"]').val(); //上牌时间
		var iLicheng = $('input[name="lichengs"]').val(); //里程数
		var iSheng = $('select[name="sheng"]').val(); //省
		var iShi = $('select[name="shi"]').val(); //市
		var iXian = $('select[name="xian"]').val(); //县
		var sAddr = $('input[name="xiangxidz"]').val(); //地址
		var dGouche = $('input[name="gouchesj"]').val(); //购车时间
		var iQipai = $('input[name="qipaij"]').val(); //起拍价格
		var sPaimai = $('select[name="paimaifs"]').val(); //拍卖方式
		var dStart = $('input[name="kaishisj"]').val(); //开始时间
		var dEnd = $('input[name="jieshusj"]').val(); //结束时间
		var aPicList ='[]';  //string or [] ?; 
		var sLianxir = $('input[name="lianxir"]').val(); //联系人
		var iLianxidh = $('input[name="lianxidh"]').val(); //联系电话
		var iYanzheng = $('input[name="yanzhengm"]').val(); //验证码
		var sShangPin = $('textarea[name="shangpinxq"]').val(); //商品信息

		var v = new Validator();

			oEmptyJson = {
				bMachName:v.required(sMachName),
				bPinpai:v.required(sPinpai),
				bChengxing:v.required(sCheXing),
				bShangpai:v.required(dShangPai),
				bLicheng:v.required(iLicheng),
				bSheng:v.required(iSheng),
				bShi:v.required(iShi),
				bXian:v.required(iXian),
				bAddr:v.required(sAddr),
				bGouche:v.required(dGouche),
				bQipai:v.required(iQipai),
				bPaimai:v.required(sPaimai),
				bStart:v.required(dStart),
				bEnd:v.required(dEnd),
				bPic:v.required(aPicList),
				bLianxir:v.required(sLianxir),
				bLianxidh:v.required(iLianxidh),
				bYanzheng:v.required(iYanzheng),
				bShangpin:v.required(sShangPin)
			};

			console.log(oEmptyJson);

			for (var arr in oEmptyJson)
			{
				if(!oEmptyJson[arr])
				{
					$('.am-modal-bd').text('请填写完整');
					$modal.modal();
					setTimeout(function () {
						$modal.modal('close');
					},iWiating);
					break;
					return;
				}
			}

			

		 $.ajax({
		 	url: Domain+'/ec/usedAuction/doSaveUsedAuction.do',
		 	type: 'POST',
		 	dataType: 'json',
		 	data: {
		 		machName:sMachName,
		 		brandId:sPinpai,
		 		modelId:sCheXing,
		 		buyDate:dGouche,
		 		licenseDate:dShangPai,
		 		driverMileage:iLicheng,
		 		provinceId:iSheng,
		 		cityId:iXian,
		 		districtId:iXian,
		 		address:sAddr,
		 		startTime:dStart,
		 		endTime:dEnd,
		 		auctionTypeId:sPaimai,
		 		auctionPrice:iQipai,
		 		picLists:aPicList,
		 		contact:sLianxir,
		 		contactTel:iLianxidh,
		 		auctionContent:sShangPin,
		 		code:iYanzheng
		 	},
		 })
		 .done(function() {
		 	console.log("success");
		 })
		 .fail(function() {
		 	console.log("error");
		 })
		 .always(function() {
		 	console.log("complete");
		 });

	});

	//图片上传
	webupload('btn','img-box',9);


	//获取页面数据
	getPageData();
	 
	 function getPageData (Filter)
	 {
	 	var Filter = Filter;

	 	$.ajax({  //获得所有品牌
	 		url: Domain+'/ec/brand/doSearchAllBrandList.do',
	 		type: 'GET'
	 	})
	 	.done(function(res) {

		 		if(!res.success){
		
			    $('.am-modal-bd').text(res.errorMessage);
					$modal.modal();
					setTimeout(function () {
						$modal.modal('close');
					},iWiating);

			  }
			  else{

			  	 renderTmp('#pinpailist', 'pinpailisttpl',res);
			  }
	 	});



	 	$.ajax({  //获得所有型号
	 		url: Domain+'/ec/machModel/doSearchAllMachModelList.do',
	 		type: 'GET'
	 	})
	 	.done(function(res) {

		 		if(!res.success){
		
			    $('.am-modal-bd').text(res.errorMessage);
					$modal.modal();
					setTimeout(function () {
						$modal.modal('close');
					},iWiating);

			  }
			  else{

			  	 renderTmp('#chexinglist', 'chexinglisttpl',res);

			  }
	 	});


	 };


	  //获取层级
	 laodAreas ({});
	 
	 function laodAreas (Filter)
	 {
	 	getJson(Domain+'/common/doLoadAllArea.do', Filter, function (res) {

		  if(!res.success){
	
		    $('.am-modal-bd').text(res.errorMessage);
				$modal.modal();
				setTimeout(function () {
					$modal.modal('close');
				},iWiating);

		  }
		  else{

		  	for (var i = 0; i< res.data.length; i++)
		  	{
		  		$('#provincelist').append('<option value="'+res.data[i].value+'">'+res.data[i].text+'</option>');
		  	}

		  	oMenuData = res.data;

	  	 	 //createCityList(0,oMenuData);

		  }
		},function (error) {

			$('.am-modal-bd').text(language.ajaxFailed);
							$modal.modal();
						setTimeout(function () {
							$modal.modal('close');
						},iWiating);

		});
	 }

	//生成对应城市层级
	function createCityList(index,data)
	{
		$('#citylist').empty().append('<option value="" selected>市</option>');

			for (var i = 0 ; i < data[index].children.length; i++)
		  	{
		  		$('#citylist').append('<option value="'+data[index].children[i].value+'">'+data[index].children[i].text+'</option>');
		  	}

		 	createAreaList(data[index].children[0]);
	}

	//生成对应区域数据
	function createAreaList(data)
	{
		$('#arealist').empty().append('<option value="" selected>县</option>');

		for (var i = 0 ; i < data.children.length; i++)
	  	{
	  		$('#arealist').append('<option value="'+data.children[i].value+'">'+data.children[i].text+'</option>');
	  	}
	}


	//省份市级联动
	$('#provincelist').on('change',function () {

		iProvinceIndex = $(this).find('option').index($(this).find('option:selected'));
		iProvinceIndex = parseInt(iProvinceIndex)-1;

		if(iProvinceIndex==-1)
		{
			$('#citylist').empty().append('<option value="" selected>市</option>');
			$('#arealist').empty().append('<option value="" selected>县</option>');
			return;
		}

		console.log('change');
		createCityList(iProvinceIndex,oMenuData);

	});

	//市级区域联动
	$('#citylist').on('change',function () {

		iCityIndex = $(this).find('option').index($(this).find('option:selected'));
		iCityIndex = parseInt(iCityIndex)-1;

		if(iCityIndex==-1)
		{
			$('#arealist').empty().append('<option value="" selected>县</option>');
			return;
		}

		createAreaList(oMenuData[iProvinceIndex].children[iCityIndex]);

	});

	//区域查询
	$('#arealist').on('change',function () {

		iAreaIndex = $(this).find('option').index($(this).find('option:selected'));
		iAreaIndex = parseInt(iAreaIndex)-1;

		if(iAreaIndex==-1)
		{
			return;
		}

	});


})();